package com.ruoyi.home.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.home.service.MiniprogramGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 小程序代码生成控制器
 * 
 * @author ruoyi
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/miniprogram/generator")
public class MiniprogramGeneratorController extends BaseController
{
    @Autowired
    private MiniprogramGeneratorService generatorService;

    /**
     * 生成小程序代码预览
     */
    @PostMapping("/preview")
    public AjaxResult generatePreview(@RequestBody Map<String, Object> pageData)
    {
        try {
            Map<String, String> codeFiles = generatorService.generateMiniProgramCode(pageData);
            return success(codeFiles);
        } catch (Exception e) {
            logger.error("生成小程序代码预览失败", e);
            return error("生成失败: " + e.getMessage());
        }
    }

    /**
     * 下载小程序代码包
     */
    @PostMapping("/download")
    @Log(title = "小程序代码生成", businessType = BusinessType.EXPORT)
    public void downloadCode(@RequestBody Map<String, Object> pageData, HttpServletResponse response)
    {
        try {
            generatorService.downloadMiniProgramCode(pageData, response);
        } catch (IOException e) {
            logger.error("下载小程序代码包失败", e);
        }
    }

    /**
     * 保存小程序页面设计草稿
     */
    @PostMapping("/draft/save")
    @Log(title = "保存设计草稿", businessType = BusinessType.INSERT)
    public AjaxResult saveDraft(@RequestBody Map<String, Object> draftData)
    {
        try {
            Long draftId = generatorService.saveDraft(draftData);
            return success().put("message", "草稿保存成功").put("draftId", draftId);
        } catch (Exception e) {
            logger.error("保存设计草稿失败", e);
            return error("保存失败: " + e.getMessage());
        }
    }

    /**
     * 加载设计草稿列表
     */
    @GetMapping("/draft/list")
    public AjaxResult getDraftList(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize)
    {
        try {
            return success(generatorService.getDraftList(pageNum, pageSize));
        } catch (Exception e) {
            logger.error("获取草稿列表失败", e);
            return error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 加载指定草稿内容
     */
    @GetMapping("/draft/{draftId}")
    public AjaxResult getDraft(@PathVariable Long draftId)
    {
        try {
            Map<String, Object> draft = generatorService.getDraft(draftId);
            return success(draft);
        } catch (Exception e) {
            logger.error("获取草稿内容失败", e);
            return error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 删除设计草稿
     */
    @DeleteMapping("/draft/{draftId}")
    @Log(title = "删除设计草稿", businessType = BusinessType.DELETE)
    public AjaxResult deleteDraft(@PathVariable Long draftId)
    {
        try {
            generatorService.deleteDraft(draftId);
            return success("删除成功");
        } catch (Exception e) {
            logger.error("删除草稿失败", e);
            return error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 获取可用的组件数据（轮播图、商品等）
     */
    @GetMapping("/components/data")
    public AjaxResult getComponentsData(@RequestParam String type,
                                        @RequestParam(required = false) Long campusId)
    {
        try {
            Object data = generatorService.getComponentData(type, campusId);
            return success(data);
        } catch (Exception e) {
            logger.error("获取组件数据失败", e);
            return error("获取失败: " + e.getMessage());
        }
    }
} 